/*******************************************************************
 * Copyright (C) yel_hb-USTC, 2012                                
 *                                                                  
 * FILE NAME             :  OjaRule.cpp                           
 * PRINCIPAL AUTHOR      :  yel_hb                               
 * MODULE NAME           :  OjaRule                             
 * LANGUAGE              :  C++                                      
 * TARGET ENVIRONMENT    :  windows                                   
 * DATE OF FIRST RELEASE :  2013/1/11                              
 * DESCRIPTION           :  implement the functions deifned in the 
 *							file OjaRule.h
 ******************************************************************/

/*
 * Revision log:
 *
 * Created by yel_hb,2013/1/11
 *
 */
	
#include "OjaRule.h"
#include "dmvector.h"
#include "utils.h"

OjaRule::OjaRule ()
{
	N = 0;
	eta = 0.01;
}

OjaRule::OjaRule ( int N, double eta )
{
	this->N = N;
	this->eta = eta;
	W = DMVector ( this->N );
}

void OjaRule::init ()
{
	int j;
	for ( j = 0; j < N; ++j )
	{
		W.set ( j, gaussianNormal() );
	}
}

void OjaRule::update ( DMVector X )
{
	int i;
	double y;
	DMVector tmpW, tmpX;
	tmpW = W;
	tmpX = X;
	y = W.dot ( X );
	tmpW.mul ( y );
	tmpX.sub ( tmpW );

	for ( i = 0; i < N; ++i )
	{
		W.set ( i, W.get ( i ) + eta * y * tmpX.get ( i ));
	}

}